// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.common.controller;

import com.liuxinlong.api.LDAPAuth;
import com.liuxinlong.api.SPOInterface;
import com.liuxinlong.api.SharePoint;
import com.liuxinlong.api.ThreeColorInterface;
import com.liuxinlong.api.WeChatMessageInterface;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.request.UpdateMatterReq;
import com.liuxinlong.modules.common.controller.request.UpdateReadStateReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.common.controller.response.QueryNoticeResp;
import com.liuxinlong.modules.common.service.CommonService;
import com.liuxinlong.modules.dao.DataPushDao;
import com.liuxinlong.modules.entity.DataPushRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通用功能控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private CommonService commonService;

    @Autowired
    private SPOInterface spoInterface;

    @Autowired
    private SharePoint sharePoint;

    @Autowired
    private LDAPAuth ldapAuth;

    @Autowired
    private ThreeColorInterface threeColorInterface;

    @Autowired
    private WeChatMessageInterface weChatMessageInterface;

    @Autowired
    private DataPushDao dataPushDao;

    /**
     * 生成标准编号
     *
     * @return 新生成编号
     */
    @GetMapping("generate-code")
    public BaseResp generateStandardCode() {
        Map<String, Object> resultMap = new HashMap<>();
        String code = commonService.generateStandardCode();
        resultMap.put("code", code);
        BaseResp resp = new BaseResp();
        resp.setDataMap(resultMap);
        resp.setRetMessage("生成标准编号成功！");
        return resp;
    }

    /**
     * 生成ecr编号
     *
     * @return 新生成编号
     */
    @GetMapping("generate-ecr")
    public BaseResp generateEcrCode() {
        Map<String, Object> resultMap = new HashMap<>();
        String code = commonService.generateEcrCode();
        resultMap.put("code", code);
        BaseResp resp = new BaseResp();
        resp.setDataMap(resultMap);
        resp.setRetMessage("生成ECR编号成功！");
        return resp;
    }

    /**
     * 生成ecn编号
     *
     * @return 新生成编号
     */
    @GetMapping("generate-ecn")
    public BaseResp generateEcnCode() {
        Map<String, Object> resultMap = new HashMap<>();
        String code = commonService.generateEcnCode();
        resultMap.put("code", code);
        BaseResp resp = new BaseResp();
        resp.setDataMap(resultMap);
        resp.setRetMessage("生成ECN编号成功！");
        return resp;
    }

    /**
     * 获取用户头像等信息
     *
     * @return 通知信息
     */
    @PostMapping("user-header")
    public BaseResp queryUserHeader(@RequestBody QueryBaseReq req) {
        Map<String, Object> dataMap = commonService.queryUserHeader(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataMap(dataMap);
        resp.setRetMessage("获取用户头像信息成功！");
        return resp;
    }

    /**
     * 获取通知信息
     *
     * @return 通知信息
     */
    @GetMapping("notices")
    public QueryNoticeResp generateNotices() {
        List<Map<String, Object>> dataList = commonService.getNotices();
        QueryNoticeResp resp = new QueryNoticeResp();
        resp.setData(dataList);
        resp.setTotalNum(2);
        resp.setSuccess(true);
        resp.setRetMessage("获取通知信息成功！");
        return resp;
    }

    /**
     * 修改通知未读状态
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("read-change")
    public BaseResp changeReadState(@RequestBody UpdateReadStateReq req) {
        commonService.changeReadState(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改通知未读状态信息成功！");
        return resp;
    }

    /**
     * 修改待办信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("matter-deal")
    public BaseResp dealNeedMatter(@RequestBody UpdateMatterReq req) {
        commonService.dealNeedMatter(req.getId(),req.getRemark());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改待办信息成功！");
        return resp;
    }

    /**
     * 清空未读信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("read-clear")
    public BaseResp clearReadState(@RequestBody UpdateReadStateReq req) {
        commonService.clearReadState(req.getTitle());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("清空" + req.getTitle() + "信息成功！");
        return resp;
    }

    /**
     * 获取在线人数信息
     *
     * @return 在线人数信息
     */
    @GetMapping("online-count")
    public BaseResp getOnlineCount() {
        int onlineNum = commonService.getOnlineCount();
        BaseResp resp = new BaseResp();
        resp.setTotalNum(onlineNum);
        resp.setRetMessage("获取在线人数信息成功！");
        return resp;
    }

    /**
     * 关注、取消关注用户
     *
     * @param  req 请求参数
     */
    @PostMapping("follow")
    public BaseResp followUser(@RequestBody QueryBaseReq req) {
        commonService.followUser(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("关注信息修改成功！");
        return resp;
    }

    /**
     * 测试
     *
     * @return 通知信息
     */
    @GetMapping("test")
    public BaseResp test() {
//        User user = new User();
//        user.setId("1");
//        user.setEmail(sharePoint.getUsername());
//        user.setPassword(sharePoint.getPassword());
//        SPORequestHeaderDto spo = spoInterface.connectSPO(user);
//        List<Map<String,Object>> resultList = spoInterface.searchFilePost(spo,"测试");
//        SPORequestService.searchFileGet(spo,"测试");
//        spoInterface.downloadFile(spo, "/sites/devTest/Shared Documents/2022", "a27.csv");
//        String url = spoInterface.uploadFile(spo,"/Shared Documents/研发部门","/SPO/download/德克士.pdf","德克士.pdf");
//        ldapAuth.ady("liuxinlong@hotwater.com.cn","AOS_lxl1992");
//        threeColorInterface.pushIncrementalData();
//        String localPath = "D:/code/rd-platform-back/uploadCHIMI/73319469609914368.png";
//        weChatMessageInterface.uploadTemplateMedia(localPath);
        List<Map<String, String>> dataList = new ArrayList<>();
        List<DataPushRecord> pendingList = dataPushDao.getPendingList();
        for (DataPushRecord dataPushRecord : pendingList) {
            Map<String, String> item = new HashMap<>();
            item.put("pdf_id", dataPushRecord.getContentOne());
            item.put("pdf_name", dataPushRecord.getContentTwo());
            dataList.add(item);
        }
        threeColorInterface.pushIncrementalFileData(dataList);
        BaseResp resp = new BaseResp();
        Map<String, Object> map = new HashMap<>();
//        map.put("url",url);
        resp.setDataMap(map);
        return resp;
    }

    /**
     * 获取项目详细信息
     *
     * @param req 请求信息
     * @return 项目详细信息
     */
    @PostMapping("project-detail")
    public BaseResp getProjectDetail(@RequestBody BaseDeleteReq req) {
        Map<String, Object> dataMap = commonService.getProjectDetail(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataMap(dataMap);
        resp.setRetMessage("获取项目详细信息成功！");
        return resp;
    }

}
